package me.zhengjie.wechat.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.utils.ResponseBean;
import me.zhengjie.wechat.domain.AdvertisingType;
import me.zhengjie.wechat.service.AdvertisingTypeService;
import me.zhengjie.wechat.service.dto.AdvertisingTypeQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

/**
* @author Jiang yunxiao
* @date 2019-11-10
*/
@Api(tags = "AdvertisingType管理")
@RestController
@RequestMapping("/api/advertisingType")
public class AdvertisingTypeController {

    private final AdvertisingTypeService advertisingTypeService;

    public AdvertisingTypeController(AdvertisingTypeService advertisingTypeService) {
        this.advertisingTypeService = advertisingTypeService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('advertisingType:list')")
    public void download(HttpServletResponse response, AdvertisingTypeQueryCriteria criteria) throws IOException {
        advertisingTypeService.download(advertisingTypeService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询AdvertisingType")
    @ApiOperation("查询AdvertisingType")
    @PreAuthorize("@el.check('advertisingType:list')")
    public ResponseEntity getAdvertisingTypes(AdvertisingTypeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(advertisingTypeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增AdvertisingType")
    @ApiOperation("新增AdvertisingType")
    @PreAuthorize("@el.check('advertisingType:add')")
    public ResponseEntity create(@Validated @RequestBody AdvertisingType resources){
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        resources.setCreationTime((ts) );
        return new ResponseEntity<>(advertisingTypeService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改AdvertisingType")
    @ApiOperation("修改AdvertisingType")
    @PreAuthorize("@el.check('advertisingType:edit')")
    public ResponseEntity update(@Validated @RequestBody AdvertisingType resources){
        advertisingTypeService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping(value = "/{id}")
    @Log("删除AdvertisingType")
    @ApiOperation("删除AdvertisingType")
    @PreAuthorize("@el.check('advertisingType:del')")
    public ResponseEntity delete(@PathVariable Long id){
        advertisingTypeService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
    /**
     *
     * 功能描述: 查询广告类型List
     * @param:
     * @return:
     * @auther: JiangYunXiao
     * @date: 2019/11/11 22:20
     */
    @GetMapping("/getAdList")
    @Log("查询广告类型")
    @ApiOperation("查询广告类型")
    public ResponseBean getAdList(){
        ResponseBean responseBean = new ResponseBean();
        responseBean.setCode("0000");
        responseBean.setData(advertisingTypeService.getList());
        return responseBean;
    }
}